from google.appengine.ext import db
from domain import comment
from domain import topic
from domain import discussion
from repository import commentrepository
from repository import topicrepository
from repository import discussionrepository
from repository import userprofilerepository
from task import articletask
from task import documenttask
from config import sitesettings
from task import basetask
import datetime

class DiscussionTask(basetask.BaseTask):

    def getAllowedTypes(self):
        settings = sitesettings.SiteSettings()
        return settings.discussiontypes

    def getDiscussionByType(self, type):
        repository = discussionrepository.DiscussionRepository()
        discussions = repository.getByType(type)
        self.logmessage("DiscussionTask retrieved discusions by type: " + str(type))
        return discussions

    def getTopicsByDiscussion(self, type):
        repository = topicrepository.TopicRepository()
        topics = repository.getByDiscussion(discussion)
        self.logmessage("DiscussionTask retrieved topics by discussion: " + str(type))
        return topics

    def getCurrentTopics(self, howmany):
        repository = topicrepository.TopicRepository()
        topics = repository.getCurrent(howmany)
        self.logmessage("DiscussionTask retrieved most current topics, limit = " + str(howmany))
        return topics

    def saveDiscussion(self, newdiscussion):
        if (isinstance(newdiscussion, discussion.Discussion)):
            repository = discussionrepository.DiscussionRepository()
            if (repository.save(newdiscussion)):
                self.logmessage("DiscussionsTask saved discussion: " + str(newdiscussion.title))
                return True
            else:
                self.logmessage("DiscussionTask could not save discussion: " + str(newdiscussion.title))
                return False
        else:
            return False

    def deleteDiscussion(self, olddiscussion):
        if (isinstance(olddiscussion, dicussion.Discussion)):
            repository = discussionrepository.DiscussionRepository()
            repository.delete(olddiscussion)
            self.logmessage("Discussion deleted: " + str(olddiscussion.title))

    def getDiscussionByKey(self, discussionkey):
        thekey = db.Key(discussionkey)
        repository = discussionrepository.DiscussionRepository()
        return repository.getByKey(thekey)

    def getTopicsByDiscussion(self, discussion):
        repository = topicrepository.TopicRepository()
        self.logmessage("DiscussionTask retrieved topics by discussion: " + str(discussion.title))
        return repository.getByDiscussion(discussion)

    def getTopicByKey(self, topickey):
        thekey = db.Key(topickey)
        repository = topicrepository.TopicRepository()
        return repository.getByKey(thekey)

    def saveTopic(self, newtopic, currentuserprofile):
        if (isinstance(newtopic, topic.Topic)):
            repository = topicrepository.TopicRepository()
            if (repository.save(newtopic)):
                self.incrementTopicCounters(newtopic)
                self.incrementProfileCommentCounters(currentuserprofile)
                self.logmessage("DiscussionsTask saved topic: " + str(newtopic.title))
                return True
            else:
                self.logmessage("DiscussionTask could not save topic: " + str(newtopic.title))
                return False
        else:
            return False

    def deleteTopic(self, oldtopic):
        if (isinstance(oldtopic, topic.Topic)):
            repository = topicrepository.TopicRepository()
            self.decrementTopicCounters(oldtopic)
            repository.delete(oldtopic)
            self.logmessage("Topic deleted: " + str(oldtopic.title))

    def getCommentByKey(self, commentkey):
        thekey = db.Key(commentkey)
        repository = commentrepository.CommentRepository()
        return repository.getByKey(thekey)

    def getCommentsByTopic(self, topic):
        repository = commentrepository.CommentRepository()
        self.logmessage("DiscussionTask retrieved comments by topic: " + str(topic.title))
        return repository.getByTopic(topic)

    def saveComment(self, newcomment, currentuserprofile):
        if (isinstance(newcomment, comment.Comment)):
            repository = commentrepository.CommentRepository()
            if (repository.save(newcomment)):
                self.incrementTopicCommentCounters(newcomment.topic)
                self.incrementProfileCommentCounters(currentuserprofile)
                self.logmessage("DiscussionsTask saved comment for topic: " + str(newcomment.topic.title))
                return True
            else:
                self.logmessage("DiscussionsTask could not save comment for topic: " + str(newcomment.topic.title))
                return False
        else:
            return False

    def deleteComment(self, oldcomment):
        if (isinstance(oldcomment, comment.Comment)):
            repository = commentrepository.CommentRepository()
            self.decrementTopicCommentCounters(oldcomment.topic)
            repository.delete(oldcomment)
            self.logmessage("DiscussionsTask deleted comment for topic: " + str(oldcomment.topic.title))

    def getTopicsByArticle(self, article):
        repository = topicrepository.TopicRepository()
        self.logmessage("DiscussionsTask retrieved topics by article: " + str(article.title))
        return repository.getByArticle(article)

    def incrementTopicCounters(self, topic):
        if (topic.type == 'Article'):
            topic.article.commentcount = topic.article.commentcount + 1
            task = articletask.ArticleTask()
            task.save(topic.article)
        if (topic.type == 'Document'):
            topic.document.commentcount = topic.document.commentcount + 1
            task = documenttask.DocumentTask()
            task.save(topic.document)
        if (topic.type == 'Discussion'):
            topic.discussion.topiccount = topic.discussion.topiccount + 1
            self.saveDiscussion(topic.discussion)

    def decrementTopicCounters(self, topic):
        if (topic.type == 'Article'):
            topic.article.commentcount = topic.article.commentcount - 1
            if (topic.article.commentcount < 0):
                topic.article.commentcount = 0
            task = articletask.ArticleTask()
            task.save(topic.article)
        if (topic.type == 'Document'):
            topic.document.commentcount = topic.document.commentcount - 1
            if (topic.document.commentcount < 0):
                topic.document.commentcount = 0
            task = documenttask.DocumentTask()
            task.save(topic.document)
        if (topic.type == 'Discussion'):
            topic.discussion.topiccount = topic.discussion.topiccount - 1
            if (topic.discussion.topiccount < 0):
                topic.discussion.topiccount = 0
            self.saveDiscussion(topic.discussion)

    def incrementProfileCommentCounters(self, userprofile):
        userprofile.lastpost = datetime.datetime.now()
        userprofile.commentcounter = userprofile.commentcounter + 1
        repository = userprofilerepository.UserProfileRepository()
        repository.save(userprofile)

    def incrementTopicCommentCounters(self, topic):
        topic.commentcount = topic.commentcount + 1
        repository = topicrepository.TopicRepository()
        repository.save(topic)
        if (topic.type == 'Article'):
            topic.article.commentcount = topic.article.commentcount + 1
            task = articletask.ArticleTask()
            task.save(topic.article)
        if (topic.type == 'Document'):
            topic.document.commentcount = topic.document.commentcount + 1
            task = documenttask.DocumentTask()
            task.save(topic.document)
        if (topic.type == 'Discussion'):
            topic.discussion.commentcount = topic.discussion.commentcount + 1
            self.saveDiscussion(topic.discussion)

    def decrementTopicCommentCounters(self, topic):
        topic.commentcount = topic.commentcount - 1
        if (topic.commentcount < 0):
            topic.commentcount = 0
        repository = topicrepository.TopicRepository()
        repository.save(topic)
        if (topic.type == 'Article'):
            topic.article.commentcount = topic.article.commentcount - 1
            if (topic.article.commentcount < 0):
                topic.article.commentcount = 0
            task = articletask.ArticleTask()
            task.save(topic.article)
        if (topic.type == 'Document'):
            topic.document.commentcount = topic.document.commentcount - 1
            if (topic.document.commentcout < 0):
                topic.document.commentcount = 0
            task = documenttask.DocumentTask()
            task.save(topic.document)
        if (topic.type == 'Discussion'):
            topic.discussion.commentcount = topic.discussion.commentcount - 1
            if (topic.discussion.commentcount < 0):
                topic.discussion.commentcount = 0
            self.saveDiscussion(topic.discussion)

